package junior.db.dao;

import junior.db.dao.base.IBaseId;
import junior.db.dao.mybatis.base.BaseSelectMapper;
import junior.db.dao.mybatis.mapper.EntityMapper;
import junior.db.dao.mybatis.type.ResultType;
import junior.db.dao.query.AbstractQuery;
import junior.db.dao.query.EntityQuery;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wubo
 * @date 2024/9/6 14:30
 */
class MybatisSelectMapperImpl<E extends IBaseId> implements BaseSelectMapper<E> {
    protected EntityMapper entityMapper;
    protected Class<E> entityClass;
    
    MybatisSelectMapperImpl(EntityMapper entityMapper, Class<E> entityClass) {
        this.entityMapper = entityMapper;
        this.entityClass = entityClass;
    }
    
    @Override
    public Long count(AbstractQuery query) {
        return entityMapper.count(convertToEntityQuery(query));
    }
    
    @Override
    public E get(AbstractQuery query) {
        return entityMapper.get(convertToEntityQuery(query));
    }
    
    @Override
    public <T> T getForObject(AbstractQuery query, ResultType<T> resultType) {
        return entityMapper.getForObject(convertToEntityQuery(query), resultType);
    }
    
    @Override
    public Map<String, Object> getForMap(AbstractQuery query) {
        return entityMapper.getForMap(convertToEntityQuery(query));
    }
    
    @Override
    public String getForString(AbstractQuery query) {
        return entityMapper.getForString(convertToEntityQuery(query));
    }
    
    @Override
    public Integer getForInteger(AbstractQuery query) {
        return entityMapper.getForInteger(convertToEntityQuery(query));
    }
    
    @Override
    public Long getForLong(AbstractQuery query) {
        return entityMapper.getForLong(convertToEntityQuery(query));
    }
    
    @Override
    public Boolean getForBoolean(AbstractQuery query) {
        return entityMapper.getForBoolean(convertToEntityQuery(query));
    }
    
    @Override
    public BigDecimal getForBigDecimal(AbstractQuery query) {
        return entityMapper.getForBigDecimal(convertToEntityQuery(query));
    }
    
    @Override
    public Date getForDate(AbstractQuery query) {
        return entityMapper.getForDate(convertToEntityQuery(query));
    }
    
    @Override
    public LocalDate getForLocalDate(AbstractQuery query) {
        return entityMapper.getForLocalDate(convertToEntityQuery(query));
    }
    
    @Override
    public LocalDateTime getForLocalDateTime(AbstractQuery query) {
        return entityMapper.getForLocalDateTime(convertToEntityQuery(query));
    }
    
    @Override
    public List<E> list(AbstractQuery query) {
        return entityMapper.list(convertToEntityQuery(query));
    }
    
    @Override
    public <T> List<T> listForObject(AbstractQuery query, ResultType<T> resultType) {
        return entityMapper.listForObject(convertToEntityQuery(query), resultType);
    }
    
    @Override
    public List<Map<String, Object>> listForMap(AbstractQuery query) {
        return entityMapper.listForMap(convertToEntityQuery(query));
    }
    
    @Override
    public List<String> listForString(AbstractQuery query) {
        return entityMapper.listForString(convertToEntityQuery(query));
    }
    
    @Override
    public List<Integer> listForInteger(AbstractQuery query) {
        return entityMapper.listForInteger(convertToEntityQuery(query));
    }
    
    @Override
    public List<Long> listForLong(AbstractQuery query) {
        return entityMapper.listForLong(convertToEntityQuery(query));
    }
    
    @Override
    public List<Boolean> listForBoolean(AbstractQuery query) {
        return entityMapper.listForBoolean(convertToEntityQuery(query));
    }
    
    @Override
    public List<BigDecimal> listForBigDecimal(AbstractQuery query) {
        return entityMapper.listForBigDecimal(convertToEntityQuery(query));
    }
    
    @Override
    public List<Date> listForDate(AbstractQuery query) {
        return entityMapper.listForDate(convertToEntityQuery(query));
    }
    
    @Override
    public List<LocalDate> listForLocalDate(AbstractQuery query) {
        return entityMapper.listForLocalDate(convertToEntityQuery(query));
    }
    
    @Override
    public List<LocalDateTime> listForLocalDateTime(AbstractQuery query) {
        return entityMapper.listForLocalDateTime(convertToEntityQuery(query));
    }
    
    protected EntityQuery convertToEntityQuery(AbstractQuery query) {
        if (query instanceof EntityQuery) {
            return (EntityQuery) query;
        }
        EntityQuery eq = new EntityQuery(entityClass, query.getPrefix());
        eq.fromFilterParam(query);
        return eq;
    }
}
